package itheima.zuoye.Test3;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        String info = "10001,张无忌,男,2023-07-22 11:11:12,东湖-黄鹤楼#" +
                "10002,赵敏,女,2023-07-22 09:11:21,黄鹤楼-归元禅寺#" +
                "10003,周芷若,女,2023-07-22 04:11:21,木兰文化区-东湖#" +
                "10004，小昭,女,2023-07-22 08:11:21,东湖#" +
                "10005，灭绝,女,2023-07-22 17:11:21,归元禅寺" ;

        List<Student> students = parseInfo(info);
        students.forEach(System.out::println);

        // 业务二：统计每个景点选择的次数
        Map<String, Long> choiceCount = countChoice(students);
        choiceCount.forEach((k, v) -> System.out.println("景点: " + k + ", 选择次数: " + v));

        // 业务三：找出想去人数最多的景点以及没有选择这个景点的学生
        findMostPopularChoiceAndStudentsNotChosenIt(students, choiceCount);
    }

    private static List<Student> parseInfo(String info) {
        List<Student> students = new ArrayList<>();
        String normalizedInfo = info.replace("，", ",");
        String[] studentInfos = normalizedInfo.split("#");

        for (String studentInfo : studentInfos) {
            // 统一将中文逗号替换为英文逗号

            String[] parts = studentInfo.split(",");

            try {
                Long id = Long.parseLong(parts[0]);
                String name = parts[1];
                String sex = parts[2];
                LocalDateTime chooseTime = LocalDateTime.parse(parts[3],
                        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

                List<String> choices = Arrays.asList(parts[4].split("-"));
                students.add(new Student(id, name, sex, chooseTime, choices));
            } catch (NumberFormatException e) {
                System.err.println("数据格式错误: " + studentInfo);
                e.printStackTrace();
            }
        }
        return students;
    }

    private static Map<String, Long> countChoice(List<Student> students) {
        Map<String, Long> choiceCount = new HashMap<>();
        for (Student student : students) {
            for (String choice : student.getChoices()) {
                choiceCount.put(choice, choiceCount.getOrDefault(choice, 0L) + 1);
            }
        }
        return choiceCount;
    }

    private static void findMostPopularChoiceAndStudentsNotChosenIt(List<Student> students, Map<String, Long> choiceCount) {
        // 找出想去人数最多的景点
        String mostPopularChoice = choiceCount.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);

        System.out.println("最多人想去的景点: " + mostPopularChoice);

        // 找出没有选择这个景点的学生
        List<Student> studentsNotChosenIt = students.stream()
                .filter(student -> !student.getChoices().contains(mostPopularChoice))
                .collect(Collectors.toList());

        System.out.println("没有选择这个景点的学生:");
        studentsNotChosenIt.forEach(System.out::println);
    }
}
